using System;
using Org.BouncyCastle.Crypto.Digests;
using Umi.Crypto.Abstraction.HashAlgo;
using Umi.Web.Metadatas.Attributes;

namespace Umi.Web.Crypto.Hash
{
    [Service("MD5Hash")]
    public class Md5Service : IHashService
    {
        public string Name => "MD5";

        public byte[] ComputeHash(byte[] data)
        {
            MD5Digest md5 = new MD5Digest();
            byte[] output = new byte[md5.GetDigestSize()];
            md5.BlockUpdate(data, 0, data.Length);
            md5.DoFinal(output, 0);
            return output;
        }
    }

    [Service("SHA1Hash")]
    public class Sha1Service : IHashService
    {
        public string Name => "SHA-1";

        public byte[] ComputeHash(byte[] data)
        {
            Sha1Digest sha1 = new Sha1Digest();
            byte[] rtn = new byte[sha1.GetDigestSize()];
            sha1.BlockUpdate(data, 0, data.Length);
            sha1.DoFinal(rtn, 0);
            return rtn;
        }
    }

    [Service("SHA224Hash")]
    public class Sha224Service : IHashService
    {
        public string Name => "SHA-224";

        public byte[] ComputeHash(byte[] data)
        {
            Sha224Digest sha224 = new Sha224Digest();
            byte[] rtn = new byte[sha224.GetDigestSize()];
            sha224.BlockUpdate(data, 0, data.Length);
            sha224.DoFinal(rtn, 0);
            return rtn;
        }
    }

    [Service("SHA256Hash")]
    public class Sha256Service : IHashService
    {
        public string Name => "SHA-256";

        public byte[] ComputeHash(byte[] data)
        {
            Sha256Digest sha256 = new Sha256Digest();
            byte[] rtn = new byte[sha256.GetDigestSize()];
            sha256.BlockUpdate(data, 0, data.Length);
            sha256.DoFinal(rtn, 0);
            return rtn;
        }
    }

    [Service("SHA384Hash")]
    public class Sha384Service : IHashService
    {
        public string Name => "SHA-384";

        public byte[] ComputeHash(byte[] data)
        {
            Sha384Digest sha384 = new Sha384Digest();
            byte[] rtn = new byte[sha384.GetDigestSize()];
            sha384.BlockUpdate(data, 0, data.Length);
            sha384.DoFinal(rtn, 0);
            return rtn;
        }
    }

    [Service("SHA512Hash")]
    public class Sha512Service : IHashService
    {
        public string Name => "SHA-512";

        public byte[] ComputeHash(byte[] data)
        {
            Sha512Digest sha512 = new Sha512Digest();
            byte[] rtn = new byte[sha512.GetDigestSize()];
            sha512.BlockUpdate(data, 0, data.Length);
            sha512.DoFinal(rtn, 0);
            return rtn;
        }
    }
}
